<template>
    <div class="login_page fillcontain"
         style="background-image: url(https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1551779563098&di=d048fbc833fae69fff4cdde9b95e7dcb&imgtype=0&src=http%3A%2F%2Fbpic.588ku.com%2Fback_pic%2F04%2F96%2F41%2F43592ae02c2eed1.jpg);background-size: contain;background-repeat: round">
        <transition name="form-fade" mode="in-out">
            <section class="form_contianer" v-show="showLogin">
                <div class="manage_tip">
                    <p>Monitor统一信息平台</p>
                </div>
                <el-form :model="loginForm" :rules="rules" ref="loginForm">
                    <el-form-item prop="username">
                        <el-input v-model="loginForm.username" placeholder="用户名"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input type="password" placeholder="密码" show-password
                                  v-model="loginForm.password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('loginForm')" class="submit_btn">登录</el-button>
                    </el-form-item>
                </el-form>
            </section>
        </transition>
    </div>
</template>

<script>
    import {api_login} from '@/api/api'

    export default {
        data() {
            return {
                loginForm: {
                    username: 'admin',
                    password: 'admin',
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ],
                },
                showLogin: false,
            }
        },
        mounted() {
            this.showLogin = true;
            if (localStorage.getItem("token")) {
                this.$router.push('home')
            }
        },
        computed: {},
        methods: {
            async submitForm(formName) {
                this.$refs[formName].validate(async (valid) => {
                    if (valid) {
                        const res = await api_login({
                            username: this.loginForm.username,
                            password: this.loginForm.password
                        })
                        console.log(res)
                        if (res.code == 0) {
                            this.$message({
                                type: 'success',
                                message: '登录成功'
                            });
                            localStorage.setItem("token", res.token)
                            console.log(localStorage.getItem("token"))
                            this.$router.push('home')
                        } else {
                            this.$message({
                                type: 'error',
                                message: res.msg
                            });
                        }
                    } else {
                        this.$notify.error({
                            title: '错误',
                            message: '请输入正确的用户名密码',
                            offset: 100
                        });
                        return false;
                    }
                });
            },
        },
        watch: {}
    }
</script>

<style lang="less" scoped>
    @import '../style/mixin';

    .login_page {
        /*background: #baf5ff;*/
        /*background: "./../assets/bg.jpg";*/
        /*background-color: #f09825;*/
    }

    .manage_tip {
        position: absolute;
        width: 100%;
        top: -80px;
        left: 0;
        p {
            font-size: 40px;
            color: black;
        }
    }

    .form_contianer {
        .wh(420px, 180px);
        .ctp(420px, 180px);
        padding: 25px;
        border-radius: 5px;
        text-align: center;
        background-color: #fff;
        .submit_btn {
            width: 100%;
            font-size: 16px;
        }
    }
</style>
